package modes

import (
	"fmt"
	"time"
)

/*
 * 描述: 业绩表
 * 表名: group_goals
 *
 ********************************************************************/
type GroupGoals struct {
	Id        int64  `json:"g_id" xorm:"id" form:"g_id"`                                          //ID
	Uid       int64  `json:"g_uid" xorm:"uid" form:"uid" binding:"required"`                      //组员ID
	TeamId    int64  `json:"g_team_id" xorm:"team_id" form:"team_id"`                             //组ID
	UName     string `json:"g_uname" xorm:"uname" form:"uname" binding:"required"`                //姓名
	StartTime int64  `json:"g_start_time" xorm:"start_time" form:"start_time" binding:"required"` //开始时间
	EndTime   int64  `json:"g_end_time" xorm:"end_time" form:"end_time" binding:"required"`       //结束时间
	Balance   int64  `json:"g_balance" xorm:"balance" form:"balance" binding:"required"`          //目标完成金额
	RealPrice int    `json:"g_realprice" xorm:"real_price" form:"real_price"`                     //实际完成余额
	At        int64  `json:"g_at" xorm:"at"`                                                      //时间
}

func (this *GroupGoals) TableName() string {
	return "group_goals"
}

func (this *GroupGoals) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(2).Insert(this)
}

func (this *GroupGoals) Get() (bool, error) {
	var val GroupGoals
	where := fmt.Sprintf("uid = %d and end_time >= %d", this.Uid, this.StartTime)
	return Db(2).Where(where).Get(&val)
}

func (this *GroupGoals) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}
func (this *GroupGoals) SetPrice(field string) (int64, error) {
	where := fmt.Sprintf("uid = %d", this.Uid)
	return this.update(where, field)
}

func (this *GroupGoals) update(where string, field string) (int64, error) {
	return Db(2).Where(where).Cols(field).Update(this)
}

func (this *GroupGoals) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(2).Where(where).Delete(this)
}
